#pragma once

#include "AdaptationData.h"
#include "../Core/Log.h"
#include "../Core/System.h"

template <class T> // T must be a descendant of AdaptationType!
class AdaptationMap : public std::map<std::string, T>
{
protected:
	std::string m_FileName;

public:
	AdaptationMap(std::string p_FileName)
	{
		m_FileName = p_FileName;
		ParseFile();
	}

	virtual ~AdaptationMap()
	{
	}

	virtual void ParseFile()
	{
		std::fstream t_FStream(m_FileName.c_str());
		if(!t_FStream)
		{
			std::cerr << "File <" << m_FileName << "> is not found" << std::endl;
			return;
		}
		t_FStream.close();
		std::map<std::string, T>::clear();
		AdaptationData t_AD(m_FileName);
		std::multimap<std::string, std::string> t_Blank;
		std::vector<Dictionary> t_Result = t_AD.search(t_Blank);
		for(std::vector<Dictionary>::iterator p=t_Result.begin(); p!=t_Result.end(); p++)
		{
			T t_T;
			t_T.ParseDictionary(*p);
			insert(std::pair<std::string, T>(t_T.Key(), t_T));
			//t_T.Print();
		}
		Core::Log::GetSingleton().Write(Core::LogLevel_Info, "File [" + m_FileName + "] loaded with [" + Core::System::ToString(t_Result.size()) + "] data(s)");
	}

	T* GetByKey(std::string p_Key)
	{
		T* t_T = NULL;
		if(std::map<std::string, T>::count(p_Key)>0)
		{
			t_T = &std::map<std::string, T>::find(p_Key)->second;
		}
		return t_T;
	}
};
